package com.handpay.nocardpay.baselibrary.qualificationinfo.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.handpay.nocardpay.baselibrary.commbase.model.IBaseModel;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorInfo;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseResponseBean;
import com.handpay.nocardpay.baselibrary.config.NoCardPayApi;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.main.mine.view.IICertNoExporyView;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.IDCardStatus;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.QualificationQueryBean;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.QueryCertNoExporyBean;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.ThreeElementResult;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.impl.QualificationModelImpl;
import com.handpay.nocardpay.baselibrary.qualificationinfo.view.IIDCardSupplementView;
import com.handpay.nocardpay.baselibrary.qualificationinfo.view.IQualificationPictureView;
import com.handpay.nocardpay.baselibrary.qualificationinfo.view.IQualificationStatusView;
import com.handpay.nocardpay.baselibrary.qualificationinfo.view.IQualificationView;

import java.util.HashMap;

/**
 * Created by fplei on 2017/8/13.
 * 资质提交相关业务
 */

public class QualificationPresenterImpl implements QualificationPresenter {
    /**
     * 填写资质信息页面
     */
    private IQualificationView mqualificationView = null;
    /**
     * 上传资质图片页面
     */
    private IQualificationPictureView mQualificationPictureView = null;
    /**
     * 资质审核状态页面
     */
    private IQualificationStatusView iQualificationStatusView = null;
    /**
     * 证件补传页面
     */
    private IIDCardSupplementView iidCardSupplementView = null;
    /**
     * 证件状态
     */
    private IICertNoExporyView iiCertNoExporyView = null;

    private QualificationModelImpl qualificationPresenter = null;

    //基本信息界面
    public QualificationPresenterImpl(Context context, IQualificationView qualificationView) {
        this.mqualificationView = qualificationView;
        qualificationPresenter = new QualificationModelImpl(context);
    }

    //资质信息图片上传界面
    public QualificationPresenterImpl(Context context, IQualificationPictureView iQualificationPictureView) {
        this.mQualificationPictureView = iQualificationPictureView;
        qualificationPresenter = new QualificationModelImpl(context);
    }

    //资质信息状态页面
    public QualificationPresenterImpl(Context context, IQualificationStatusView miQualificationStatusView) {
        this.iQualificationStatusView = miQualificationStatusView;
        qualificationPresenter = new QualificationModelImpl(context);
    }

    public QualificationPresenterImpl(Context context, IQualificationView qualificationView, IQualificationStatusView miQualificationStatusView) {
        this.mqualificationView = qualificationView;
        this.iQualificationStatusView = miQualificationStatusView;
        qualificationPresenter = new QualificationModelImpl(context);
    }

    //证件补件界面
    public QualificationPresenterImpl(Context context, IIDCardSupplementView miidCardSupplementView) {
        this.iidCardSupplementView = miidCardSupplementView;
        qualificationPresenter = new QualificationModelImpl(context);
    }

    //证件状态
    public QualificationPresenterImpl(Context context, IICertNoExporyView miiCertNoExporyView) {
        this.iiCertNoExporyView = miiCertNoExporyView;
        qualificationPresenter = new QualificationModelImpl(context);
    }

    @Override
    public void checkCardThreeElements(HashMap<String, Object> paramers, boolean isShowLoading) {
        showLoading(isShowLoading);
        qualificationPresenter.checkCardThreeElements(paramers, ThreeElementResult.class, new IBaseModel.OnRequestFinishedListener<ThreeElementResult>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                hiddenLoading();
                if (mqualificationView != null) {
                    mqualificationView.showErrorAlert(baseErrorInfo);
                }
            }

            @Override
            public void onSuccess(ThreeElementResult response) {
                hiddenLoading();
                if (mqualificationView != null) {
                    if (response.isRequstSuccess())
                        mqualificationView.checkCardThreeElementsSuccess(response);
                    else
                        mqualificationView.checkCardThreeElementsFail(response.errMessage);
                }
            }
        });
    }

    @Override
    public BaseResponseBean uploadQualificationPicture(HashMap<String, String> paramers, byte[] imgByte, boolean isShowLoading) {
        return qualificationPresenter.uploadFile(paramers, BaseResponseBean.class, imgByte, NoCardPayApi.Do_UploadPicInfo);
    }

    @Override
    public BaseResponseBean uploadIDCardSupplementPicture(HashMap<String, String> paramers, byte[] imgByte, boolean isShowLoading) {
        return qualificationPresenter.uploadFile(paramers, BaseResponseBean.class, imgByte, NoCardPayApi.Do_UpdateMerchantCertInfo);
    }

    @Override
    public void submitData(HashMap<String, Object> paramers, boolean isShowLoading) {
        showLoading(isShowLoading);
        qualificationPresenter.submitQualificationData(paramers, BaseResponseBean.class, new IBaseModel.OnRequestFinishedListener<BaseResponseBean>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                hiddenLoading();
                if (mQualificationPictureView != null) {
                    mQualificationPictureView.showErrorAlert(baseErrorInfo);
                }
            }

            @Override
            public void onSuccess(BaseResponseBean response) {
                hiddenLoading();
                if (mQualificationPictureView != null) {
                    if (response.isRequstSuccess())
                        mQualificationPictureView.submmitQualificationSuccess();
                    else
                        mQualificationPictureView.submmitQualificationFail(response.errMessage);
                }
            }
        });
    }

    @Override
    public void queryQualificationInfo(HashMap<String, Object> paramers, boolean isShowLoading) {
        showLoading(isShowLoading);
        qualificationPresenter.queryQualificationCheckInfo(paramers, QualificationQueryBean.class, new IBaseModel.OnRequestFinishedListener<QualificationQueryBean>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                hiddenLoading();
                if (iQualificationStatusView != null) {
                    iQualificationStatusView.showErrorAlert(baseErrorInfo);
                }
            }

            @Override
            public void onSuccess(QualificationQueryBean response) {
                hiddenLoading();
                if (iQualificationStatusView != null) {
                    if (response.isRequstSuccess()) {
                        decryQualificationInfoData(response);
                        iQualificationStatusView.queryQualificationInfoSuccess(response);
                    } else {
                        iQualificationStatusView.queryFail(response.errMessage);
                    }
                }
            }
        });
    }

    @Override
    public void queryIDCardSupplimentInfo(HashMap<String, Object> paramers, boolean isShowLoading) {
        showLoading(isShowLoading);
        qualificationPresenter.queryIDCardInfo(paramers, IDCardStatus.class, new IBaseModel.OnRequestFinishedListener<IDCardStatus>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                hiddenLoading();
                if (iidCardSupplementView != null) {
                    iidCardSupplementView.showErrorAlert(baseErrorInfo);
                }
            }

            @Override
            public void onSuccess(IDCardStatus response) {
                hiddenLoading();
                if (iidCardSupplementView != null) {
                    if (response.isRequstSuccess()) {
                        HPLog.i(HPLog.LFP_TAG, "response:" + response.toString());
                        decryIDCardSupplimentInfoData(response);
                        iidCardSupplementView.submitIDCardSupplementSuccess(response);
                    } else {
                        iidCardSupplementView.submitIDCardSupplementFail(response.errMessage);
                    }
                }
            }
        });
    }

    @Override
    public void queryCertNoExpory(HashMap<String, Object> paramers, boolean isShowLoading) {
        showLoading(isShowLoading);
        qualificationPresenter.queryCertNoExpory(paramers, QueryCertNoExporyBean.class, new IBaseModel.OnRequestFinishedListener<QueryCertNoExporyBean>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                hiddenLoading();
                if (iiCertNoExporyView != null) {
                    iiCertNoExporyView.showErrorAlert(baseErrorInfo);
                }
            }

            @Override
            public void onSuccess(QueryCertNoExporyBean response) {
                hiddenLoading();
                if (iiCertNoExporyView != null) {
                    if (response.isRequstSuccess()) {
//                        response.setRemindMessage(SecureManager.getInstance().des(response.getRemindMessage(),0,null));
                        iiCertNoExporyView.queryCertNoSuccess(response);
                    } else {
                        iiCertNoExporyView.queryCertNoFail(response.errMessage);
                    }
                }
            }
        });
    }

    /**
     * 查询资质信息数据解密
     *
     * @param qualificationQueryBean
     */
    private void decryQualificationInfoData(QualificationQueryBean qualificationQueryBean) {
        String temp_name = qualificationQueryBean.getName();
        if (!TextUtils.isEmpty(temp_name)) {
            qualificationQueryBean.setName(SecureManager.getInstance().des(temp_name, 0, null));
        }
        String temp_phone = qualificationQueryBean.getPhone();
        if (!TextUtils.isEmpty(temp_phone)) {
            qualificationQueryBean.setPhone(SecureManager.getInstance().des(temp_phone, 0, null));
        }
        String temp_certno = qualificationQueryBean.getCertNo();
        if (!TextUtils.isEmpty(temp_certno)) {
            qualificationQueryBean.setCertNo(SecureManager.getInstance().des(temp_certno, 0, null));
        }
        String temp_card = qualificationQueryBean.getCardNum();
        if (!TextUtils.isEmpty(temp_card)) {
            qualificationQueryBean.setCardNum(SecureManager.getInstance().des(temp_card, 0, null));
        }
        String temp_bankname = qualificationQueryBean.getBankName();
        if (!TextUtils.isEmpty(temp_bankname)) {
            qualificationQueryBean.setBankName(SecureManager.getInstance().des(temp_bankname, 0, null));
        }
        String bankofdeposit = qualificationQueryBean.getBankOfDeposit();
        if (!TextUtils.isEmpty(bankofdeposit)) {
            qualificationQueryBean.setBankOfDeposit(SecureManager.getInstance().des(bankofdeposit, 0, null));
        }
        String temp_sharecode = qualificationQueryBean.getRecommender();
        if (!TextUtils.isEmpty(temp_sharecode)) {
            qualificationQueryBean.setRecommender(SecureManager.getInstance().des(temp_sharecode, 0, null));
        }
    }

    /**
     * 解密查询证件补件审核信息
     *
     * @param idCardStatus
     */
    private void decryIDCardSupplimentInfoData(IDCardStatus idCardStatus) {
        String auditInfo = idCardStatus.getAuditInfo();
        if (!TextUtils.isEmpty(auditInfo))
            idCardStatus.setAuditInfo(SecureManager.getInstance().des(auditInfo, 0, null));
        String name = idCardStatus.getName();
        if (!TextUtils.isEmpty(name))
            idCardStatus.setName(SecureManager.getInstance().des(name, 0, null));
        String certNo = idCardStatus.getCertNo();
        if (!TextUtils.isEmpty(certNo))
            idCardStatus.setCertNo(SecureManager.getInstance().des(certNo, 0, null));
        String frontCCPic = idCardStatus.getFrontCCPic();
        if (!TextUtils.isEmpty(frontCCPic)) idCardStatus.setFrontCCPic(frontCCPic);
        String backCCPic = idCardStatus.getBackCCPic();
        if (!TextUtils.isEmpty(backCCPic)) {
            idCardStatus.setBackCCPic(backCCPic);
        }
        String holdPic = idCardStatus.getHoldPic();
        if (!TextUtils.isEmpty(holdPic)) {
            idCardStatus.setHoldPic(holdPic);
        }
    }

    private void showLoading(boolean isShow) {
        if (mqualificationView != null) {
            if (isShow) mqualificationView.showProgress();
        }
        if (mQualificationPictureView != null) {
            if (isShow) mQualificationPictureView.showProgress();
        }
        if (iQualificationStatusView != null) {
            if (isShow) {
                iQualificationStatusView.showProgress();
            }
        }
        if (iidCardSupplementView != null) {
            if (isShow) iidCardSupplementView.showProgress();
        }
        if (iiCertNoExporyView != null) {
            if (isShow) iiCertNoExporyView.showProgress();
        }
    }

    private void hiddenLoading() {
        if (mqualificationView != null) {
            mqualificationView.hideProgress();
        }
        if (mQualificationPictureView != null) {
            mQualificationPictureView.hideProgress();
        }
        if (iQualificationStatusView != null) {
            iQualificationStatusView.hideProgress();
        }
        if (iidCardSupplementView != null) {
            iidCardSupplementView.hideProgress();
        }
        if (iiCertNoExporyView != null) {
            iiCertNoExporyView.hideProgress();
        }
    }

    @Override
    public void onDestory() {
        mqualificationView = null;
        mQualificationPictureView = null;
        iQualificationStatusView = null;
        iidCardSupplementView = null;
        iiCertNoExporyView = null;
        qualificationPresenter = null;
    }
}
